<script src="__PUBLIC__/myadmin/js/tinymce/tinymce.min.js">
</script>

<!-- <script src="__PUBLIC__/myadmin/js/jquery.uploadify.js">
</script>
<script src="__PUBLIC__/myadmin/js/jquery.uploadify.min.js">
</script>
<script src="__PUBLIC__/myadmin/js/jquery.form.js">
</script> -->

<!-- <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> -->
<script type="text/javascript">
 $(document).ready(function () {
        var tinymceEditor;
        tinymce.init({
            selector: "textarea",

            // we override default upload handler to simulate successful upload
            images_upload_handler: function (blobInfo, success, failure) {
              setTimeout(function() {
                var xhr, formData;

                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', '{:U("Products/update")}');

                xhr.onload = function() {
                  var json; 
                  console.log(xhr);   
                  if (xhr.status != 200) {
                    failure('HTTP Error: ' + xhr.status);
                    return;
                  }

                  json = JSON.parse(xhr.responseText);

                  if(!json || json['code']=='error'){
                    failure(json['msg']);
                    return;
                  }
                };

                formData = new FormData();
                formData.append('file', blobInfo.blob(), blobInfo.filename());
                xhr.send(formData);
                xhr.onreadystatechange=function()
                {
                  if (xhr.readyState==4 && xhr.status==200)
                  {
                    var info = JSON.parse(xhr.responseText);
                    console.log(info);
                    if(info['code'] == 'success'){
                      success(info['url']);
                    }
                  }
                }
              }, 2000);
            },

            auto_focus: "content",
            language: "zh_CN",
            theme: "modern",
            plugins: [
                "advlist autolink lists image charmap preview",
                "searchreplace visualblocks",
                "insertdatetime media contextmenu paste",
                "emoticons textcolor",
                'image code',
            ],
            toolbar1: "undo redo | styleselect | fontselect | fontsizeselect | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent",
            toolbar2: "preview | forecolor backcolor  | image media ",
            //TinyMCE 会将所有的 font 元素转换成 span 元素
            convert_fonts_to_spans: true,
            //换行符会被转换成 br 元素
            convert_newlines_to_brs: false,
            //在换行处 TinyMCE 会用 BR 元素而不是插入段落
            force_br_newlines: false,
            //当返回或进入 Mozilla/Firefox 时，这个选项可以打开/关闭段落的建立
            force_p_newlines: false,
            //这个选项控制是否将换行符从输出的 HTML 中去除。选项默认打开，因为许多服务端系统将换行转换成 <br />，因为文本是在无格式的 textarea 中输入的。使用这个选项可以让所有内容在同一行。
            remove_linebreaks: false,
            //不能把这个设置去掉，不然图片路径会出错
            relative_urls: false,
            //不允许拖动大小
            resize: false,

            font_formats: "宋体=宋体;黑体=黑体;仿宋=仿宋;楷体=楷体;隶书=隶书;幼圆=幼圆;Arial=arial,helvetica,sans-serif;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats",
            fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt"
        });
    });
</script>



<script type="text/javascript">
  $(function() {
    // tinymce.init({
    //   selector: 'textarea',
    //   language: "zh_CN"
    // });
    $("#logo-preview").click(function() {
      $("#logo").click(); //隐藏了input:file样式后，点击头像就可以本地上传
      $("#logo").on("change", function() {
        $(".tips.logo").show();
        $("#logo-preview").attr("src", '');
        var formData = new FormData();
        formData.append('file', this.files[0]);
        $.ajax({
          url: "{:U('file/upload')}",
          type: 'POST',
          cache: false,
          data: formData,
          processData: false,
          contentType: false
        }).done(function(res) {
          console.log(res);
          var photo = res.result[0];
          $("#logo-preview").attr("src", photo + '!315x315');
          $("input#logo-img").val(photo);
          $(".tips.logo").hide();
          //$('#logo li').removeClass('weui-uploader__file_status');
          //$('#logo img').attr('src', photo);
        }).fail(function(res) {});
      });
    });
    $("#photos").on("change", function() {
      var formData = new FormData();
      $(".tips.photos").show();
      console.log('test')
      $.each(this.files, function(index, file) {
        formData.append('files[]', file);
      })
      //formData.append('files[]', this.files);
      $.ajax({
        url: "{:U('file/upload')}",
        type: 'POST',
        cache: false,
        data: formData,
        processData: false,
        contentType: false
      }).done(function(res) {
        console.log(res);
        var photo = res.result[0];
        $.each(res.result, function(index, file) {
          if (file) {
            var div = $('<div class="col-sm-1" onclick="deletePhoto(this)"><div class="uploadimg" id="uploadimg"><img src="' + file + '!315x315" alt=""><input type="hidden" name="photos[]" value="' + file + '"></div></div>');
            $('#photos-preview').append(div);
          }
        });
        $(".tips.photos").hide();
        //$('#logo li').removeClass('weui-uploader__file_status');
        //$('#logo img').attr('src', photo);
      }).fail(function(res) {});

    });
  });


  function deletePhoto(obj) {
    $(obj).remove();
  }

  function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
      url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
      url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
      url = window.webkitURL.createObjectURL(file);
    }
    return url;
  }
</script>